package net.dipper.tankwar.game.control;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import wnb.util.control.Command;

import net.dipper.tankwar.core.config.ConfigManager;
import net.dipper.tankwar.core.data.ObjectMap;
import net.dipper.tankwar.game.ui.GameContext;

public class GameControl {
	private static final long MOVE_DELAY = 200;
	
	private GameContext parent;
	private ObjectMap map;
	private long lastOperateTimeStamp;
	private ConfigManager config;
	
	public GameControl(GameContext parent, ObjectMap map){
		try {
			this.parent = parent;
			this.config = ConfigManager.getInstance();
			this.map = map;
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | ParserConfigurationException
				| SAXException | IOException e) {
			e.printStackTrace();
		}
	}

	public void left() {
		Command command = new MoveLeftCommand(this, map);
		parent.pushCommand(command);
	}

	public void right() {
		Command command = new MoveRightCommand(this, map);
		parent.pushCommand(command);
	}

	public void up() {	
		Command command = new MoveUpCommand(this, map);
		parent.pushCommand(command);
	}

	public void down() {
		Command command = new MoveDownCommand(this, map);
		parent.pushCommand(command);
	}

	public void shoot() {
		Command command = new ShootCommand(this, map);
		parent.pushCommand(command);
	}
	
	public ConfigManager getConfig(){
		return this.config;
	}

	//判断是否可以射击
	public boolean canOperate() {
		long current = System.currentTimeMillis();
		return current - lastOperateTimeStamp > MOVE_DELAY;
	}

	//设置上一次操作时间
	public void setLastOperateTimeStamp(long timeStamp){
		this.lastOperateTimeStamp = timeStamp;
	}
	
	//重新设置视域位置
	public void resetViewPort(){
		parent.resetViewPort();
	}
}
